﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Domain.Repositories;
using Abp.ObjectMapping;
using Abp.Runtime.Caching;
using AutoMapper.QueryableExtensions;
using FwProject.Core.Restaurant;
using FwProject.Restaurant.Dto;
using FwProject.Users;
using X.PagedList;

namespace FwProject.Restaurant
{
    [AbpAuthorize]
    public class StockCheckInfoAppService : FwProjectAppServiceBase, IStockCheckInfoAppService
    {
        public readonly IRepository<StockCheckInfo, Guid> _repositoryStockCheck;
        public readonly IRepository<RestaurantInfo, Guid> _repositoryRestaurantInfo;
        public readonly IUserAppService _userAppService;
        public readonly ICacheManager _catchmanager;
        public IGuidGenerator _guid { get; set; }
        public IObjectMapper _map { get; set; }

        public StockCheckInfoAppService(ICacheManager catchmanager, IRepository<StockCheckInfo, Guid> repositoryStockCheck, IUserAppService userAppService,
            IRepository<RestaurantInfo, Guid> repositoryRestaurantInfo)
        {
            _repositoryRestaurantInfo = repositoryRestaurantInfo;
            _catchmanager = catchmanager;
            _repositoryStockCheck = repositoryStockCheck;
            _userAppService = userAppService;

            _guid = SequentialGuidGenerator.Instance;
        }
        /// <summary>
        /// 工厂修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Guid> Create(StockCheckInput input)
        {
            var model = _map.Map<StockCheckInfo>(input);
            model.Id = _guid.Create();
            model.FactoryinfoId = (await GetCurrentUserAsync()).FactoryinfoId;

            var rmodel = _repositoryRestaurantInfo.Get(input.RestaurantId);
            rmodel.RealStock = input.CheckCount;
            // rmodel.StockTime = DateTime.Now;

            return await _repositoryStockCheck.InsertAndGetIdAsync(model);
        }


        /// <summary>
        /// 工厂修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Guid> Delete(Guid input)
        {
            var model = _map.Map<StockCheckInfo>(input);
            model.Id = _guid.Create();
            model.FactoryinfoId = (await GetCurrentUserAsync()).FactoryinfoId;

            //var rmodel= _repositoryRestaurantInfo.Get(input.RestaurantId);
            // rmodel.Stock = input.CheckCount;
            // rmodel.StockTime = DateTime.Now;

            return await _repositoryStockCheck.InsertAndGetIdAsync(model);
        }

        /// <summary>
        /// 根据当前工厂查询
        /// </summary>
        /// <param name="pageIdx"></param>
        /// <param name="pageSize"></param>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <param name="restaurantName"></param>
        /// <param name="person"></param>
        /// <returns></returns>
        public async Task<IPagedList<StockCheckDto>> GetPagedListAsync(int pageIdx, int pageSize, DateTime? beginTime, DateTime? endTime, string restaurantName, string person)
        {
            var usermodel = await _userAppService.GetByIdUserAsync(AbpSession.UserId.Value);

            var listmodel = _repositoryStockCheck.GetAllIncluding(o => o.RestaurantInfoModel).AsQueryable();

            if (beginTime.HasValue)
                listmodel = listmodel.Where(o => o.CreationTime >= beginTime);
            if (endTime.HasValue)
                listmodel = listmodel.Where(o => o.CreationTime <= endTime.Value.AddHours(24));
            if (!string.IsNullOrEmpty(restaurantName))
                listmodel = listmodel.Where(o => o.RestaurantInfoModel.RestaurantName.Contains(restaurantName));
            if (!string.IsNullOrEmpty(person))
                listmodel = listmodel.Where(o => o.Person == person);
            return await listmodel.Where(o => o.FactoryinfoId == usermodel.FactoryinfoId).OrderByDescending(o => o.CreationTime).ProjectTo<StockCheckDto>().ToPagedListAsync(pageIdx, pageSize);
        }

        /// <summary>
        /// 获取盘点单实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<StockCheckDto> GetEntityAsync(Guid id)
        {
            var entity = await _repositoryStockCheck.GetAsync(id);
            return entity.MapTo<StockCheckDto>();
        }

        /// <summary>
        /// 更新盘点数量
        /// </summary>
        /// <param name="id"></param>
        /// <param name="CheckCount"></param>
        /// <returns></returns>
        public async Task ModifyAsync(Guid id, int CheckCount)
        {
            var entity = await _repositoryStockCheck.GetAsync(id);
            var restaurant = await _repositoryRestaurantInfo.GetAsync(entity.RestaurantId);
            entity.CheckCount = CheckCount;
            entity.ProfitCount = CheckCount - entity.Count;
            restaurant.RealStock = CheckCount;
            await _repositoryStockCheck.UpdateAsync(entity);
            await _repositoryRestaurantInfo.UpdateAsync(restaurant);
        }

    }
}
